using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Configuration;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;


namespace Pixels.DataLayer
{
    internal class CommonDbHelper : DbHelper
    {
        #region Constructor
        public CommonDbHelper()
            : base(ConfigurationManager.ConnectionStrings[0].ConnectionString)
        { }
        public CommonDbHelper(string ConnectionStringName)
            : base(ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString)
        { }
        #endregion

        #region ExecuteNonQuery Methods
        public override int ExecuteNonQuery(string storedProcedure, out ArrayList outputValues, params object[] parameterValues)
        {
            int Result = -1;
            Database db;
            DbCommand Command;
            outputValues = new ArrayList();
            try
            {
                db = DatabaseFactory.CreateDatabase();
                object[] validatedParameterValues = AssignDBNull(parameterValues);
                Command = db.GetStoredProcCommand(storedProcedure, parameterValues);
                Result = db.ExecuteNonQuery(Command);
                if (Result > 0)
                {
                    DbParameterCollection Parameters = Command.Parameters;
                    for (int i = 0; i < Parameters.Count; i++)
                    {
                        if ((Parameters[i].Direction == ParameterDirection.InputOutput) || (Parameters[i].Direction == ParameterDirection.Output))
                        {
                            outputValues.Add(Parameters[i].Value);
                        }
                    }
                    Result = 0;
                }
                else
                    new ApplicationException("Data Insertion/Updation Failed.");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            return Result;
        }
        public override int ExecuteNonQuery(string commandText, params object[] parameterValues)
        {
            int Result = -1;
            Database db;
            DbCommand Command;
            try
            {
                db = DatabaseFactory.CreateDatabase();

                Command = db.GetStoredProcCommand(commandText, parameterValues);

                Result = db.ExecuteNonQuery(Command);

                if (Result > 0)
                    Result = 0;
                else
                    new ApplicationException("Data Insertion/Updation Failed");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {
            }
            return Result;
        }
        public override int ExecuteNonQuery(string commandText, CommandType commandType)
        {
            int Result = -1;
            Database db;
            DbCommand Command;
            try
            {

                db = DatabaseFactory.CreateDatabase();

                if (commandType == CommandType.StoredProcedure)
                    Command = db.GetStoredProcCommand(commandText);
                else
                    Command = db.GetSqlStringCommand(commandText);

                Command.Connection = db.CreateConnection();
                Command.Connection.Open();

                Result = Command.ExecuteNonQuery();

                if (Result > 0)
                    Result = 0;
                else
                    new ApplicationException("Data Insertion/Updation Failed.");
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            return Result;
        }
        #endregion

        #region ExecuteReader Methods
        public override IDataReader ExecuteReader(string CommandText, params object[] parameterValues)
        {
            Database db = null;
            DbCommand Command = null;
            IDataReader Reader = null;

            try
            {
                db = DatabaseFactory.CreateDatabase();

                Command = db.GetStoredProcCommand(CommandText, parameterValues);

                Reader = db.ExecuteReader(Command);

                return Reader;
            }
            catch (Exception Ex)
            {
                if (Reader != null) Reader.Close();
                throw Ex;
            }
            finally
            {

            }
        }
        public override IDataReader ExecuteReader(string commandText, CommandType commandType)
        {
            Database db = null;
            DbCommand Command = null;
            IDataReader Reader = null;
            try
            {
                db = DatabaseFactory.CreateDatabase();
                if (commandType == CommandType.Text)
                {
                    Command = db.GetSqlStringCommand(commandText);
                    Reader = db.ExecuteReader(Command);
                    return Reader;
                }
                else
                    return this.ExecuteReader(commandText, null);
            }
            catch (Exception Ex)
            {
                if (Reader != null) Reader.Close();
                throw Ex;
            }
            finally
            {

            }
        }
        #endregion

        #region ExecuteDataSet
        public override DataSet ExecuteDataSet(string storedProcedure, params object[] parameterValues)
        {
            Database db = null;
            DbCommand Command = null;
            DataSet ResultSet = null;
            try
            {
                db = DatabaseFactory.CreateDatabase();
                if (parameterValues != null)
                    Command = db.GetStoredProcCommand(storedProcedure, parameterValues);
                else
                    Command = db.GetStoredProcCommand(storedProcedure);
                ResultSet = db.ExecuteDataSet(Command);
                return ResultSet;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {

            }
        }
        public override DataSet ExecuteDataSet(string commandText, CommandType commandType)
        {
            Database db = null;
            DbCommand Command = null;
            DataSet ResultSet = null;
            try
            {
                db = DatabaseFactory.CreateDatabase();
                if (commandType == CommandType.Text)
                {
                    Command = db.GetStoredProcCommand(commandText);
                    ResultSet = db.ExecuteDataSet(Command);
                    return ResultSet;
                }
                else
                    return this.ExecuteDataSet(commandText, null);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
            finally
            {

            }
        }
        #endregion

        #region ExecuteScalar
        #endregion
    }
}
